package com.zero.system.controller;

import com.zero.system.entity.Company;
import com.zero.system.entity.Flight;
import com.zero.system.entity.Models;
import com.zero.system.service.CompanyService;
import com.zero.system.service.FlightService;
import com.zero.system.service.ModelsService;
import com.zero.system.util.AjaxResult;
import com.zero.system.util.Data;
import com.zero.system.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/manager")
public class FlightController {

    @Autowired
    private AjaxResult ajaxResult;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Autowired
    private FlightService flightService;

    @Autowired
    private ModelsService modelsService;

    @Autowired
    private CompanyService companyService;

    /**
     * 跳转航班管理页面
     * @return
     */
    @GetMapping("/flight")
    public String flight(){
        return "manager/flight/flightList";
    }

    /**
     * 异步加载航班列表
     * @param pageno
     * @param pagesize
     * @return
     */
    @RequestMapping("/flightList")
    @ResponseBody
    public Object companyList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                                @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                                String flightNo){
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",pageno);
        paramMap.put("pagesize",pagesize);
        //判断是否为空
        if(!StringUtils.isEmpty(flightNo)) {
            paramMap.put("flightNo",flightNo);
        }
        PageBean<Flight> pageBean = flightService.queryPage(paramMap);
        Map<String,Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count",pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }

    /**
     * 跳转添加或修改航班页面
     * @return
     */
    @GetMapping("/addFlight")
    public String addFlight(String type, Integer id, Model model){
        List<Company> companyList=companyService.findAll();
        List<Models> modelsList=modelsService.findAll();
        model.addAttribute("companyList",companyList);
        model.addAttribute("modelsList",modelsList);
        if(type != null && type.equals("edit")){
            Flight flight = flightService.findById(id);
            model.addAttribute("flight",flight);
        }
        return "manager/flight/addFlight";
    }

    /**
     * 添加航班  修改航班
     * @return
     */
    @PostMapping("/saveFlight")
    @ResponseBody
    public AjaxResult saveFlight(Flight flight, HttpServletRequest request) throws ParseException {
        String modelsId=request.getParameter("modelsId");
        Models models=modelsService.findById(Integer.parseInt(modelsId));
        flight.setModels(models);
        String startDate1=request.getParameter("startDate1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(!StringUtils.isEmpty(startDate1)){
            flight.setStartDate(sdf.parse(startDate1));
        }
        String endDate1=request.getParameter("endDate1");
        if(!StringUtils.isEmpty(endDate1)){
            flight.setEndDate(sdf.parse(endDate1));
        }
        if(flight.getId() !=null){
            //修改
            int count = flightService.update(flight);
            if(count > 0){
                ajaxResult.ajaxTrue("修改成功");
            }else{
                ajaxResult.ajaxFalse("修改失败");
            }
        }else{
            //添加
            flight.setFirstNum(models.getFirstNum());
            flight.setSecondNum(models.getSecondNum());
            flight.setLastNum(models.getLastNum());
            int count = flightService.add(flight);
            if(count > 0){
                ajaxResult.ajaxTrue("添加成功");
            }else{
                ajaxResult.ajaxFalse("添加失败");
            }
        }
        return ajaxResult;
    }

    /**
     * 删除
     * @param data
     * @return
     */
    @PostMapping("/delFlight")
    @ResponseBody
    public AjaxResult delCompany(Data data){
        int count = flightService.delete(data.getIds());
        if(count >= data.getIds().size()){
            ajaxResult.ajaxTrue("删除成功");
        }else{
            ajaxResult.ajaxFalse("删除失败");
        }
        return ajaxResult;
    }

}
